package com.wholesale;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.LogoutRequest;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;

public class MyAccountLayout extends LinearLayout {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private RelativeLayout loadingLayout;
	private TextView userNameView;

	// 构造函数
	public MyAccountLayout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.myaccount, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("More");
		leftButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				viewFlipper.showPrevious();
				viewFlipper.removeView(MyAccountLayout.this);
			}
		});
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setText("Logout");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);
		rightButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LogoutAsync logoutAsync = new LogoutAsync(
						MyAccountLayout.this.context);
				logoutAsync.execute((Void) null);
			}
		});
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("My Account");
		userNameView = (TextView) baseView.findViewById(R.id.username_text);
		userNameView.setText(Constants.userInfo.getUserName() + "("
				+ Constants.loginResponse.getUserLevel() + ")");
		SimpleAdapter listItemAdapter = new SimpleAdapter(context,
				WebServiceClient.getMyAccountData(),// 数据源
				R.layout.moreitem,// ListItem的XML实现
				new String[] { "text" }, new int[] { R.id.item_text });
		bodyList.setAdapter(listItemAdapter);
		bodyList.setSelector(R.drawable.more_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
	}

	class LogoutAsync extends AsyncTask<Void, Integer, Boolean> {
		ProgressDialog pdialog;

		public LogoutAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			LogoutRequest logoutRequest = new LogoutRequest();
			logoutRequest.setRequestId(Constants.loginResponse.getRequestId());
			logoutRequest.setUserId(Constants.loginResponse.getUserId());
			return WebServiceClient.logout(logoutRequest.toJsonString());
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (result) {
				Intent myIntent = new Intent();// 创建Intent对象
				myIntent.setAction("com.wholesale.utils.UpdateUIService");
				myIntent.putExtra("cmd", Constants.CMD_LOGOUT_SERVICE);
				myIntent.putExtra("from", "com.wholesale.MyAccountLayout");
				((Activity) getContext()).sendBroadcast(myIntent);
				Constants.loginResponse = null;
				Constants.userInfo = null;
				viewFlipper.showPrevious();
				viewFlipper.removeView(MyAccountLayout.this);
			} else {

			}
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	// more ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			switch (arg2) {
			case 0:
				MyOrdersLayout myOrdersLayout = new MyOrdersLayout(context,
						viewFlipper);
				viewFlipper.addView(myOrdersLayout);
				viewFlipper.showNext();
				break;
			case 1:
				MyOrderTrackingsLayout myOrderTrackingsLayout = new MyOrderTrackingsLayout(
						context, viewFlipper);
				viewFlipper.addView(myOrderTrackingsLayout);
				viewFlipper.showNext();
				break;
			case 2:
				MyAddressLayout myAddressLayout = new MyAddressLayout(context,
						viewFlipper, MyAddressLayout.MYACCOUNT, -1, null);
				viewFlipper.addView(myAddressLayout);
				viewFlipper.showNext();
				break;
			case 3:
				MyFavoritesLayout myFavoritesLayout = new MyFavoritesLayout(
						context, viewFlipper);
				viewFlipper.addView(myFavoritesLayout);
				viewFlipper.showNext();
				break;
			case 4:
				MyCommentsLayout myCommentsLayout = new MyCommentsLayout(
						context, viewFlipper);
				viewFlipper.addView(myCommentsLayout);
				viewFlipper.showNext();
				break;
			case 5:
				MyFeedBackLayout myFeedBackLayout = new MyFeedBackLayout(
						context, viewFlipper);
				viewFlipper.addView(myFeedBackLayout);
				viewFlipper.showNext();
				break;
			default:
				break;
			}
		}
	};
}
